From 05afc0d40bf4fc628537cf9d4069f30334931af0 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Tue, 25 Jan 2022 17:06:49 -0700 Subject: [PATCH] Fix ODR violations discovered when using -flto. (#824) The fedora build shows these: skytraq.cc:666:8: warning: type 'struct read_state' violates the C++ One Definition Rule [-Wodr] wbt-200.cc:127:8: note: a different type is defined in another translation unit brauniger_iq.cc:30:6: warning: type 'state_t' violates the C++ One Definition Rule [-Wodr] igc.cc:150:6: note: an enum with different value name is defined in another translation unit However, even with link time optimization I was unable to observe a problem, i.e. testo passes. There is a relevant note in https://en.cppreference.com/w/cpp/language/definition, see the note about using unnamed namespaces to resolve this issue. Also note the use of unnamed namespaces is not recommended in header files, https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL59-CPP.+Do+not+define+an+unnamed+namespace+in+a+header+file --- brauniger_iq.cc | 40 +++++++++++++++++++++------------------- igc.cc | 4 +++- skytraq.cc | 18 ++++++++++-------- wbt-200.cc | 14 ++++++++------ 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/brauniger_iq.cc b/brauniger_iq.cc index 071b6b754..8e449466c 100644 --- a/brauniger_iq.cc +++ b/brauniger_iq.cc @@ -27,25 +27,27 @@ static void* serial_handle; #define MYNAME "BRAUNIGER-IQ" #define PRESTRKNAME "PRESALTTRK" -enum state_t { - st_sync, - st_fl_num, - st_data_len, - st_ser_num, - st_pilot_name, - st_start_date, - st_start_year, - st_max_alt_1, - st_max_alt_2, - st_max_climb, - st_flight_dur, - st_log_ival, - st_start_time, - st_end_time, - st_sample_alt, - st_sample_spd, - num_states -}; +namespace { // fix ODR violation with igc + enum state_t { + st_sync, + st_fl_num, + st_data_len, + st_ser_num, + st_pilot_name, + st_start_date, + st_start_year, + st_max_alt_1, + st_max_alt_2, + st_max_climb, + st_flight_dur, + st_log_ival, + st_start_time, + st_end_time, + st_sample_alt, + st_sample_spd, + num_states + }; +} static state_t state; inline state_t& operator++(state_t& s) // prefix { diff --git a/igc.cc b/igc.cc index e17e9beab..54053cd9b 100644 --- a/igc.cc +++ b/igc.cc @@ -147,7 +147,9 @@ static void rd_deinit() gbfclose(file_in); } -enum state_t { id, takeoff, start, turnpoint, finish, landing }; +namespace { // fix ODR violation with brauniger_iq + enum state_t { id, takeoff, start, turnpoint, finish, landing }; +} inline state_t& operator++(state_t& s) // prefix { return s = static_cast(s + 1); diff --git a/skytraq.cc b/skytraq.cc index 2a15b9725..fa0d661e6 100644 --- a/skytraq.cc +++ b/skytraq.cc @@ -663,14 +663,16 @@ ECEF_to_LLA(double x, double y, long z, double* lat, double* lon, double* alt) *lon = *lon /M_PI*180; } -struct read_state { - route_head* route_head_; - unsigned wpn, tpn; - - unsigned gps_week; - unsigned gps_sec; - long x, y, z; -}; +namespace { // fix ODR violation with wbt-200 + struct read_state { + route_head* route_head_; + unsigned wpn, tpn; + + unsigned gps_week; + unsigned gps_sec; + long x, y, z; + }; +} static void state_init(struct read_state* pst) diff --git a/wbt-200.cc b/wbt-200.cc index 8cf0e5bc9..142d99901 100644 --- a/wbt-200.cc +++ b/wbt-200.cc @@ -124,12 +124,14 @@ struct buf_head { unsigned checksum; }; -struct read_state { - route_head* route_head_; - unsigned wpn, tpn; - - struct buf_head data; -}; +namespace { // fix ODR violation with skytraq + struct read_state { + route_head* route_head_; + unsigned wpn, tpn; + + struct buf_head data; + }; +} static void db(int l, const char* msg, ...) { -- 2.30.2